const axios = require(`axios`)
const express = require('express')
const app = express()
app.get('/getToken', async (req, resp) => {
  console.log(req.query);
  const query = req.query
  const cookie = query.cookie
  if(!cookie) {
    return resp.json({
      code: 500,
      message: 'cookie 空'
    })
  }
  // 请求跨域访问
  resp.setHeader('Access-Control-Allow-Origin', '*')
  let code = ''
  try {
    code = await getCode(cookie)
  } catch (error) {
    return resp.json({
      code: 500,
      message: 'cookie 非法'
    })
  }
  return resp.json({
    code: 200,
    token: code
  })
})
app.listen(9000, () => {
  console.log('服务已经启动')
})


const cookie = '__snaker__id=XR8FlRw7g15XofeQ; tokenId=230441cbbffa980ee7d19b9de6e05287; wdata4=ekkRw3iguGUviTKMI12enYv/PC5zPXj0/NhR2J2dgUSKDf+HZT0i9wWHSFjFYkoHdwxpLuLGB6aYYCHrXMiQ369SPbxU1mBM19Qg00My8eKKasJhFpjS0Ut+xXvJrkipHHluLKC1e2lKiDU5qTzbKA==; wdata3=2pohNDCkiJXiFhqFKUQUfs8QjKuoj6HWSQLAvwNJebbRozk5RJtdGYVbXqtu2M1ZCwEDKULg6bpabGa2q2iC95eL68nodMX5UBTiQwzCpjRogwat2zJYrhU4WXrrr3bVtss1DiSo; _ac=eyJhaWQiOjk0ODI4LCJjaWQiOjQwMjM0NjU0NDh9; YD00636840014594%3AWM_NI=DaBw%2B6xv%2BoaPGwORH9H2podxlYvXUtDjEAlCvnelS68YdI383m%2FFn5e7Smj5bzgb%2FuHjVpr0%2FXnnQ1FeumN05WtQQu%2F0W31JbBAHYXQ9HNaVWyX1GYHNYv2zUywyTnARdE0%3D; YD00636840014594%3AWM_NIKE=9ca17ae2e6ffcda170e2e6eed7cd63b189a3aad440928e8bb3d45f979a9b82d13aa898f7bbe662b7e8b7b2d62af0fea7c3b92a88bf8e98aa3ebcaae590e45c85a981b3cb338ceec090e774b087bab9d88088b68ba2ef49b6b2ab8dd23efc87acaab234f1b3e1a7ed3fa7e99d8ee93fa2a6a983ae7f8cb69a91f13ced88a28fd43b86bbbb8ecd3cf789bcb2d47fedf099d4b442a29e8fa9dc5493e788afe45e95a798d2c6538c929797b17297a7b99ad766838eaca6b337e2a3; YD00636840014594%3AWM_TID=7DOtjTrAVidEVRFFQEbAiIXtNg2rsmUW; wdata3=QyM2BU8dztpQisok9vobdjTg3UNTknt5PxYhmVPaSWYuLRiL6sDJ7gZHxUgPzuxjgyxzwftatUZHxgRVbfqeq1w1w5khrx5icjH5zuqEphxLHB7og1WqEishKNJ5bBGe8sxBSu; tokenId=d4b56f8a847ae073838fa0c40d88f4c1; gdxidpyhxdE=GBU4DWCoo%5CsP2NXNX12j6ds2J33w%2F2gudP3XN9qM8EnLTC%2BYgR5eqDtebB%5Cavd5PX6NpNSQVk%5CsfX7pLqDsL8wh8XwgzH6zi8Hgp6Zkri%5C%5CHEs8hl%2Flb2B7eddGPSmDSdWpKNVL28WD%2FQ6uAQQRDyifnLvSqGK3Sh%2FeTsbbR4ksUxYYd%3A1694614546282'
const validate = 'CN31_5Of5TN9Oi5JQv4lrKEdxQwqk4O1ycDN5YJfG70plEg1cHUyV.qVIOesEWxCjrKj1piB0BOxrUYwdV-KiTiD4esrfpIIAAXhrMuuQKMMVbEvfQFfUXpRQzetVgFwQYIqLW4Zfu.my2P8F1DMPJxHRk1gyGnWDHVaPN4j4W2fdkLz7YIg4dBBx_wgafc0hU-cIXzRlosxAbwt6j0xtMnS4oXEtfTlGq40jv6Qq_GHoPMJ4IfndnGJ8mb-EHCgSdpIZZJnJ.LsuTrAuLvHSvHEy90TtzFDxZK6T-Cszo0rqiALLTlCrWZB12K7bFIgIlQwmMeHVhlsnRs5iu7tIIKIM6P-teUmUM7DtIE0gGrbfjMDobADTOIkMXWs5.GSFRMcHlyLdeHdDsitoibeGKKE_unyldIYZasVjEcyLhMFfxLPdoPLYAfda_Tx1JOQ8Q_kEHjCX1_GnS16MYMliQ5cE51FWORw-ZVnOb9WPIdv7fJoM145uyIvdBNzbRHX3'
// getCode().then(code => {
//   console.log('code---', code)
//   // code = '6688'
//   axios.get(`https://94828.activity-35.m.duiba.com.cn/projectx/p1270126c/draw/draw.do?ruleId=ru_coupon_1&validate=${validate}&token=${code}&user_type=1&is_from_share=1&_t=1694617695718`, {
//     headers: headers
//   })
//     .then(resp => {
//       console.log('resp', resp.data)
//     })
// })





function getCode(cookie) {
  return new Promise(async (resolve, j) => {
    const now500 = new Date().getTime()
    await axios.get(`https://94828.activity-35.m.duiba.com.cn/projectx/p1270126c/buriedPoint?__ts__=${now500}&pageBizId=800&duiba_platform=project-x`, {
      headers: { Cookie: cookie }
    }).then(resp => {
      console.log('resp800', resp.data, now500)
    })

    const now501 = new Date().getTime()
    await axios.get(`https://94828.activity-35.m.duiba.com.cn/projectx/p1270126c/buriedPoint?__ts__=${now501}&pageBizId=801&duiba_platform=project-x&state=1`, {
      headers: { Cookie: cookie }
    }).then(resp => {
      console.log('resp801', resp.data, now501)
    })
    const getTokenKeyNow = new Date().getTime()
    axios.get(`https://94828.activity-35.m.duiba.com.cn/projectx/p1270126c/getTokenKey?_t=${getTokenKeyNow}&_t=${getTokenKeyNow}`, {
      headers: { Cookie: cookie }
    })
      .then(resp => {
        const str = resp.data
        console.log('str', str)
        if(!str) {
          return j('cookie 非法')
        }
        const codeStr = getCodeStr(str)
        
        const rootCode = getRootCode(codeStr)
        console.log('rootCode', rootCode, getTokenKeyNow)
        const getTokenNow = new Date().getTime()
        axios.get(`https://94828.activity-35.m.duiba.com.cn/projectx/p1270126c/getToken?_t=${getTokenNow}`, {
          headers: { Cookie: cookie }
        })
          .then(resp => {
            const str = resp.data.data
            const codeStr2 = getCodeStr(str)
            const code = getCodes(codeStr2, rootCode)
            console.log('code', code, getTokenNow)
            resolve(code)
          })
      })
  })
}
function getRootCode(rootCodeStr) {
  const startIndex = rootCodeStr.indexOf(`var key = '`) + 11
  const endIndex = rootCodeStr.indexOf(`';`)
  return rootCodeStr.substring(startIndex, endIndex)
}
function getCodes(codeStr, rootCode) {
  const startIndex = codeStr.indexOf(rootCode) + rootCode.length + 4
  const endIndex = codeStr.substring(codeStr.indexOf(rootCode) + rootCode.length + 4).indexOf(`";`) + startIndex
  return codeStr.substring(startIndex, endIndex)
}
function getCodeStr(str) {
  try {
    eval(str)
  } catch (error) {
  }
  const startIndex = str.indexOf('(__') + 1
  const endIndex = str.indexOf('));') + 1
  console.log('------------------------------------------')
  const strNew = str.substring(startIndex, endIndex)
  return eval(strNew)
}